home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
LIBRARY
/
PAS_0693
/
PRIMES.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-06-30
|
2KB
|
36 lines
{─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 351 of 355
From : Janos Szamosfalvi 1:343/45.0 06 Jun 93 19:30
To : Gary Morris
Subj : Prime numbers...
────────────────────────────────────────────────────────────────────────────────
|When fooling around in BASIC, I was able to write a |small program to extract
prime numbers, .... |it took about 20 mins. on my 386/40 to get prime numbers
|through 20000. I tried to come up with code to do the same |with Turbo but it
continues to elude me. Could anybody explain |how to write such a routine in
Pascal?}
{ the following routine uses a brute force approach with some
optimization; it took less than 3 minutes with a 286/12 to find
and print all primes up to 32768, about 50 seconds w/o printing
them; it becomes a bit slow when you get into a 6 digit range}
PROGRAM Primes;
VAR
number, max_div, divisor : INTEGER;
prime : BOOLEAN;
BEGIN
writeln('Primes:');
writeln('2');
FOR number := 2 TO MAXINT DO BEGIN
max_div := Round(sqrt(number) + 0.5);
prime := number MOD 2 <> 0;
divisor := 3;
WHILE prime AND (divisor < max_div) DO BEGIN
prime := number MOD divisor <> 0;
divisor := divisor + 2;
END;
IF prime THEN
writeln(number);
END;
END.